package de.velvettear.bruteforce.kernel;

import java.sql.Statement;

import de.velvettear.bruteforce.util.Log;

public class StatusReport extends Database {

	public static boolean createTable() { 
		boolean created = false;
		try {			
			Statement sqlStatement = connect().createStatement();
			
			created = sqlStatement.execute("CREATE TABLE IF NOT EXISTS " + Statics.TABLE_STATUSREPORTS +
														" (" +
														"m_nID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY," +
														"m_sWord VARCHAR(1000) NOT NULL," + 
														"m_sDone VARCHAR(1000) NOT NULL," + 
														"m_sTotal VARCHAR(1000) NOT NULL," +
														"m_sAverage VARCHAR(1000) NOT NULL,," + 
														"m_sRemaining VARCHAR(1000) NOT NULL," +
														"m_sTimestamp VARCHAR(1000) NOT NULL," +
														")");		
			
			disconnect();
		} catch (Exception e) {
			Log.error(e);
		}	
		return created;
	}
		
	private static String maskSpecialCharacters(String m_sValue) {	
		if (m_sValue == null) {
			m_sValue = "";
		}
		m_sValue = m_sValue.replaceAll("\\\\", "\\\\\\\\");
		m_sValue = m_sValue.replaceAll("\\\'", "\\\\'");
		m_sValue = m_sValue.replaceAll("\\\"", "\\\\\"");
		return m_sValue;
	}
	
	public static boolean insert(String m_sWord, String m_sDone, String m_sTotal, String m_sAverage, String m_sRemaining, String m_sTimestamp) {
		boolean inserted = false;
		
		m_sWord = maskSpecialCharacters(m_sWord);
		m_sDone = maskSpecialCharacters(m_sDone);
		m_sTotal = maskSpecialCharacters(m_sTotal);
		m_sAverage = maskSpecialCharacters(m_sAverage);
		m_sRemaining = maskSpecialCharacters(m_sRemaining);
		m_sTimestamp = maskSpecialCharacters(m_sTimestamp);
		
		try {				
			Statement sqlStatement = connect().createStatement();
						
			inserted = sqlStatement.execute("INSERT INTO " + Statics.TABLE_STATUSREPORTS + " " +
											"(m_sWord, m_sDone, m_sTotal, m_sAverage, m_sRemaining, m_sTimestamp) " +
											"VALUES " +
											"('" + m_sWord + "', " + m_sDone + ", " + m_sTotal + ", " + m_sAverage + ", " + m_sRemaining + ", " + m_sTimestamp + ")");					
			
			disconnect();
		} catch (Exception e) {
			Log.error(e);
		}
		
		return inserted;	
	}
	
}
